package core4.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

import com.fasterxml.jackson.databind.util.JSONPObject;

@RestController
public class IndexController {
	@ResponseBody
	@RequestMapping("/index/{name}")
	public String index(@PathVariable("name") String name) {
		System.out.println(name);
		return "hello index";
	}

	@RequestMapping("/getindex")
	public String getIndex(HttpServletRequest request,@RequestBody Map<String, Object> hashMap)  {
		
		String nameString=request.getParameter("keke");
		String abcd=request.getParameter("abcd");
		
		
		Set<Entry<String, Object>> entrySet = hashMap.entrySet();
		
		Iterator<Entry<String, Object>> iterator = entrySet.iterator();
		while (iterator.hasNext()) {
			Entry<String, Object> next = iterator.next();
			System.out.println(next.getKey()+"..."+next.getValue());
			
		}
		
		return nameString+abcd;
	}
	
	@GetMapping(path="/findpet",params= {"name=keke","age=18"})
	public String findPet() {
		return "pet";
	}
	
	@PostMapping("/multidata")
	public String multData(@RequestParam("name") String name) {
		return name;
	}
	
	@GetMapping("/testurl")
	public String TestUrl() throws MalformedURLException {
		
		URI uri = UriComponentsBuilder
		        .fromUriString("https://example.com/hotels/{hotel}")
		        .queryParam("aaa", "{hotela}")
		        .encode()
		        .buildAndExpand("111.html", "222")
		        .toUri();
		System.out.println(uri.toString());
		//System.out.println(uri.toURL());
		return "testUrl";
	}
	
	
	
	
	
	
}
